from django.db import models
from django.contrib.auth.models import User


class ChatMessage(models.Model):
    """聊天消息"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='chat_messages', verbose_name='用户', db_comment='用户ID')
    message = models.TextField(verbose_name='用户消息', db_comment='用户消息')
    response = models.TextField(verbose_name='AI回复', db_comment='AI回复')
    context = models.TextField(null=True, blank=True, verbose_name='上下文JSON', db_comment='上下文JSON')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        verbose_name = '聊天消息'
        verbose_name_plural = '聊天消息'
        db_table = 'zhahuo_chat_message'
        db_table_comment = '聊天消息记录表'
        ordering = ['created_at']

    def __str__(self):
        return f'{self.user.username}: {self.message[:30]}'
